package com.lvxingli.generateannotation.util;

import com.lvxingli.generateannotation.constants.ModifierAnnotationEnum;
import com.lvxingli.generateannotation.pojo.ModifierAnnotationWrapper;
import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lvxingli
 */
public final class ModifierAnnotationUtils {

    private ModifierAnnotationUtils() {
    }

    public static ModifierAnnotationWrapper createWrapperApi(String tag) {
        return createModifierAnnotationWrapper(ModifierAnnotationEnum.API, "@%s(tags = {\"%s\"})", tag);
    }

    public static ModifierAnnotationWrapper createWrapperApiOperation(String tag) {
        return createModifierAnnotationWrapper(ModifierAnnotationEnum.API_OPERATION, "@%s(\"%s\")", tag);
    }

    public static ModifierAnnotationWrapper createWrapperTag(int value) {
        return createModifierAnnotationWrapper(ModifierAnnotationEnum.TAG, "@%s(%s)", value);
    }

    public static ModifierAnnotationWrapper createWrapperApiModelProperty(String value) {
        return createModifierAnnotationWrapper(ModifierAnnotationEnum.API_MODEL_PROPERTY, "@%s(\"%s\")", value);
    }

    public static ModifierAnnotationWrapper createWrapperApiModelPropertyByFieldAnnotation(Map<String, String> map) {
        return createModifierAnnotationWrapperByFieldAnnotation(ModifierAnnotationEnum.API_MODEL_PROPERTY, "@%s(%s)", map);
    }

    public static ModifierAnnotationWrapper createWrapperApiModelPropertyByFieldClassAnnotation(Map<String, String> map) {
        return createModifierAnnotationWrapperByFieldClassAnnotation(ModifierAnnotationEnum.API_MODEL_PROPERTY, "@%s(%s)", map);
    }

    public static ModifierAnnotationWrapper createWrapperApiModel(String description) {
        return createModifierAnnotationWrapper(ModifierAnnotationEnum.API_MODEL, "@%s(description = \"%s\")", description);
    }

    private static ModifierAnnotationWrapper createModifierAnnotationWrapper(ModifierAnnotationEnum annotationEnum,
                                                                             String annotationTemplate,
                                                                             Object... args) {
        String annotationText = formatAnnotationText(annotationEnum, annotationTemplate, args);
        ModifierAnnotationWrapper modifierAnnotationWrapper = new ModifierAnnotationWrapper();
        modifierAnnotationWrapper.setAnnotationEnum(annotationEnum);
        modifierAnnotationWrapper.setAnnotationText(annotationText);
        return modifierAnnotationWrapper;
    }

    private static ModifierAnnotationWrapper createModifierAnnotationWrapperByFieldAnnotation(ModifierAnnotationEnum annotationEnum,
                                                                                              String annotationTemplate,
                                                                                              Map<String, String> map) {
//        FieldAnnotation 属性转换
        List<String> textList=new ArrayList<>();
        if (map.containsKey("chineseName")) {
            textList.add("value=" + getText(map.get("chineseName")));
        } else if (map.containsKey("value")){
            textList.add("value=" + getText(map.get("value")));
        }  else if (map.containsKey("chineseDesc")) {
            textList.add("value=" + getText(map.get("chineseDesc")));
        }
        for (String key : map.keySet()) {
            switch (key) {
                case "remarks":
                    textList.add("notes=" + getText(map.get(key)));
                    break;
                case "typeAndLength":
                    textList.add("dataType=" + getText(map.get(key)));
                    break;
                case "mustInput":
                    if ("Y".equals(map.get(key))) {
                        textList.add("required=true");
                    } else if ("N".equals(map.get(key))){
                        textList.add("required=false");
                    }
                    break;
            }
        }
        String annotationText = formatAnnotationText(annotationEnum, annotationTemplate, String.join(",",textList));
        ModifierAnnotationWrapper modifierAnnotationWrapper = new ModifierAnnotationWrapper();
        modifierAnnotationWrapper.setAnnotationEnum(annotationEnum);
        modifierAnnotationWrapper.setAnnotationText(annotationText);
        return modifierAnnotationWrapper;
    }


    private static ModifierAnnotationWrapper createModifierAnnotationWrapperByFieldClassAnnotation(ModifierAnnotationEnum annotationEnum,
                                                                                              String annotationTemplate,
                                                                                              Map<String, String> map) {
//        FieldAnnotation 属性转换
        List<String> textList=new ArrayList<>();
        if (map.containsKey("chineseName")) {
            textList.add("value=" + getText(map.get("chineseName")));
        } else if (map.containsKey("value")){
            textList.add("value=" + getText(map.get("value")));
        }  else if (map.containsKey("chineseDesc")) {
            textList.add("value=" + getText(map.get("chineseDesc")));
        }
        for (String key : map.keySet()) {
            switch (key) {
                case "remarks":
                    textList.add("notes=" + getText(map.get(key)));
                    break;
                case "typeAndLength":
                    textList.add("dataType=" + getText(map.get(key)));
                    break;
                case "mustInput":
                    if ("Y".equals(map.get(key))) {
                        textList.add("required=true");
                    } else if ("N".equals(map.get(key))){
                        textList.add("required=false");
                    }
                    break;
            }
        }
        String annotationText = formatAnnotationText(annotationEnum, annotationTemplate, String.join(",",textList));
        ModifierAnnotationWrapper modifierAnnotationWrapper = new ModifierAnnotationWrapper();
        modifierAnnotationWrapper.setAnnotationEnum(annotationEnum);
        modifierAnnotationWrapper.setAnnotationText(annotationText);
        return modifierAnnotationWrapper;
    }

    public static String getText(String text){
        return "\""+text+"\"";
    }

    private static String formatAnnotationText(ModifierAnnotationEnum annotationEnum,
                                               String annotationTemplate,
                                               Object... args) {
        Object[] modifiedArgs = ArrayUtils.addFirst(args, annotationEnum.getName());
        return String.format(annotationTemplate, modifiedArgs);
    }
}
